<?php
/**
 * Created by PhpStorm.
 * User: hui
 * Date: 2016/10/9
 * Time: 08:54
 */
namespace app\index\controller;

use app\index\model\Card;
use app\index\model\Member;
use think\Session;

class Center extends Index
{
    /**
     * 会员中心
     * @return mixed
     */
    public function index()
    {
        $session = Session::get();
        if (!isset($session['info'])){
            Session::set('info.point',$this->getPoint($session['cardID'])['AvailablePoint']);
            Session::set('info.name',$this->getMember($session['ID'])['Member']);
            $session = Session::get();
        }
        return $this->fetch('Index/center', [
            'info' => $session['info']
        ]);
    }

    public function getMember($ID)
    {
        return Member::get($ID)->toArray();
    }

    /**
     * 获取积分信息
     * @param $cardId $会员卡id
     * @return array|false|\PDOStatement|string|\think\Model
     */
    public function getPoint($cardID)
    {
        return Card::where('ID', $cardID)
            ->field('AvailablePoint')
            ->find()
            ->toArray();
    }

    /**
     * 退出
     */
    public function quit()
    {
        Session::clear();
        $this->redirect('/index');
    }
}